package com.yiren.manager.ctrl.vip;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yiren.core.exception.MyException;
import com.yiren.core.exception.SwallowException;
import com.yiren.core.utils.Favorites;
import com.yiren.core.utils.HTTPResult;
import com.yiren.core.utils.JsonMapperTool;
import com.yiren.core.utils.UUIDGenerator;
import com.yiren.entity.YrUserVipCard;
import com.yiren.entity.YrUserVipCardRent;
import com.yiren.manager.ctrl.GenericController;
import com.yiren.manager.form.vip.YrUserVipCardRentForm;
import com.yiren.manager.service.iface.vip.YrUserVipCardRentService;
import com.yiren.manager.service.iface.vip.YrUserVipCardService;
import com.yiren.manager.utils.ObjectUtils;

@Controller
@RequestMapping("userVipCardRent")
public class YrUserVipCardRentController extends GenericController<YrUserVipCardRentForm, YrUserVipCardRent>{

	@Autowired
	private YrUserVipCardRentService selfService;
	
	@Autowired
	private YrUserVipCardService userVipCardService;
	
	@RequestMapping("save")
	@ResponseBody
	public HTTPResult<YrUserVipCardRentForm> save(String data) throws SwallowException, MyException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
		HTTPResult<YrUserVipCardRentForm> result = new HTTPResult<YrUserVipCardRentForm>();
		JsonMapperTool tool = new JsonMapperTool(data);
		YrUserVipCardRentForm form = tool.mapper("userVipCardRent", YrUserVipCardRentForm.class);
		if(null==form){
			throwExp("param.error");
		}
		YrUserVipCard userVipCard = null;
		if(null==form.getUserVipCardRentId()||form.getUserVipCardRentId().isEmpty()){
			form.setUserVipCardRentId(UUIDGenerator.getUUID());
			form.setCreateTime(new Date());
		}
		form.setUpdateTime(new Date());
		YrUserVipCard rentCard = userVipCardService.findOne(form.getUserVipCardId());
		if(null==rentCard){
			throwExp("出租vip卡不存在");
		}
		//出租表
		YrUserVipCardRent userVipCardRent = ObjectUtils.objectToObject(form, YrUserVipCardRent.class);
		
		userVipCard = new YrUserVipCard();
		userVipCard.setUserVipCardId(UUIDGenerator.getUUID());
		userVipCard.setCreateId(form.getCreateId()==null?"-1":form.getCreateId());
		userVipCard.setUpdateId(form.getUpdateId());
		userVipCard.setCreateTime(new Date());
		userVipCard.setUpdateTime(new Date());
		userVipCard.setName(form.getRenterName());
		userVipCard.setSex(form.getRenterSex());
		userVipCard.setCardId(form.getRenterCardId());
		userVipCard.setLiveAddress(form.getRenterAddress());
		userVipCard.setPhoneNumber(form.getRenterPhoneNumber());
		userVipCard.setEmail(form.getRenterEmail());
		userVipCard.setVipCardName(form.getVipCardName());
		userVipCard.setRemark(form.getRemark());
		userVipCard.setStatus(2);
		userVipCard.setVipCardId(rentCard.getVipCardId());
		userVipCard.setSurplusMonths(form.getRentMonthNum());
		userVipCard.setBuckleNumber(rentCard.getBuckleNumber());
		userVipCard.setUseStatus(0);
		userVipCard.setPayCostStatus(2);
		userVipCard.setConsumptionType(rentCard.getConsumptionType());
		userVipCard.setHouseTypeId(rentCard.getHouseTypeId());
		userVipCard.setPricePerDay(rentCard.getPricePerDay());
		userVipCard.setSubsidization("无补助");//补助
		userVipCard.setSubsidizationValue("");
		userVipCard.setDiscount(Float.valueOf(0));
		userVipCard.setSubsidizationType(3);
		userVipCard.setConsumptionWay("现金");
		
		Favorites favorites = new Favorites();
		favorites.putFavorite(YrUserVipCard.class, userVipCard);
		favorites.putFavorite(YrUserVipCardRent.class, userVipCardRent);
		
		selfService.addByFavorites(favorites);
		
		return result;
	}
	
	@Override
	public YrUserVipCardRentForm toForm(YrUserVipCardRent e) throws Exception {
		YrUserVipCardRentForm form = ObjectUtils.objectToObject(e, YrUserVipCardRentForm.class);
		return form;
	}

	@Override
	public YrUserVipCardRent toEntity(YrUserVipCardRentForm f) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

}
